
export default {
	state: {
		isCollapse: true,
		tabsList:[
			{
				path: '/',
				name: 'home',
				label: '首页',
				icon: 'home'
			}
		],
		currentMenu: null,
		menu: []
	},
	mutations:{
		collapseMenu(state){
			state.isCollapse=!state.isCollapse;
		},
		setCollapse(state,val){
			state.isCollapse=val;
		},
		initTag(state){
			state.tabsList = JSON.parse(localStorage.getItem("tablist"))
		},
		selectMenu(state,val){
			if(val.name !== 'home'){
				state.currentMenu = val
				const result = state.tabsList.findIndex(item=>item.name === val.name)
				if(result === -1) {
					state.tabsList.push(val)
				}
			}
			else {
				state.currentMenu = 'home'
			}
			localStorage.setItem('tablist',JSON.stringify(state.tabsList))
		},
		closeTag(state,val){
			if(val.name!='home'){
				const result = state.tabsList.findIndex(item=>item.name===val.name)
				state.tabsList.splice(result,1)
				localStorage.setItem('tablist',JSON.stringify(state.tabsList))
			}
		},
		closeOther(state,val){
			let tabs = []
			state.tabsList.forEach(t=>{
				if(t.name=='home' || t.name==val.name){
					tabs.push(t)
				}
			})
			state.tabsList=tabs
			localStorage.setItem('tablist',JSON.stringify(state.tabsList))
		},
		closeAllTag(state){
			let length = state.tabsList.length-1
			state.tabsList.splice(1,length)
			localStorage.setItem('tablist',JSON.stringify(state.tabsList))
		},
		setMenu(state, val){
			state.menu = val
			localStorage.setItem('menu',JSON.stringify(val))
		},
		clearMenu(state){
			state.menu = []
			state.tabsList = [{path: '/',name: 'home',label: '首页',icon: 'home'}]
			localStorage.removeItem('menu')
			localStorage.setItem("tablist",state.tabsList)
		},
		addMenu(state, router){
			if(!localStorage.getItem("menu")){
				return
			}
			let menu = [];
			menu = JSON.parse(localStorage.getItem("menu"))
			state.menu = menu
			let menuArray = []
			menu.forEach(t=>{
				if(t.children){
					t.children.forEach((st)=>{
						let item = {path: st.path, name: st.name, component: () => import('../views/' + st.url)}
						menuArray.push(item)
					})
				}
				else{
					let item = {path: t.path, name: t.name, component: () => import('../views/' + t.url)}
					menuArray.push(item)
				}
			})

			//路由的动态添加
			menuArray.forEach(item =>{
				router.addRoute('Main',item)
			})

		}
	}
}